<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
require_once(dirname(__FILE__) . "/../dbcom/UserCom.php");
require_once(dirname(__FILE__) . "/../dbcom/RagionCom.php");
require_once(dirname(__FILE__) . "/../common/Common.php");
require_once(dirname(__FILE__) . "/../dbcom/WorkerCom.php");

$usercom = new UserCom();
$ragioncom = new RagionCom();
$common = new Common();
$workercom = new WorkerCom();

$cont = $_REQUEST['cont'];
switch ($cont) {
    case 1://增
        $account = $_REQUEST['account'];
        $password = $_REQUEST['password'];
        $type = $_REQUEST['type'];
        $oldaccount = $usercom->getPassword($account);
        if (!empty($oldaccount)) {
            echo json_encode(array("code" => 100, "msg" => "account has been used"));
            break;
        }
        if ($type == 0) {
            $password = md5($password);
            $res = $usercom->addUser($account, $password, $type);
            if ($res) {
                echo json_encode(array("code" => 0, "msg" => "success"));
            } else {
                echo json_encode(array("code" => 400, "msg" => "add failed"));
            }
        } else {
            $password = md5($password);
            $role = $_REQUEST['role'];
            $community = $_REQUEST['community'];
            $res = $usercom->addUser($account, $password, $type, $community, $role);
            if ($res) {
                echo json_encode(array("code" => 0, "msg" => "success"));
            } else {
                echo json_encode(array("code" => 400, "msg" => "add failed"));
            }
        }
        break;
    case 2://删
        $workerid = $_REQUEST['userid'];
        if (empty($workerid)) {
            echo json_encode(array("code" => 100, "msg" => "empty workerid"));
            break;
        }
        $res = $usercom->deleteUser($workerid);
        if ($res) {
            echo json_encode(array("code" => 0, "msg" => "success", "data" => $workerid));
        } else {
            echo json_encode(array("code" => 400, "msg" => "delete error"));
        }
        break;
    case 3://改
        $type = $_REQUEST['type'];
        $userid = $_REQUEST['userid'];
        if (empty($userid)) {
            echo json_encode(array("code" => 100, "msg" => "empty userid"));
            break;
        }
        if ($type == 0) {
            if (!isset($_REQUEST['password']) || empty($_REQUEST['password'])) {
                echo json_encode(array("code" => 0, "msg" => "success"));
            } else {
                $password = $_REQUEST['password'];
                $password = md5($password);
                $res = $usercom->updateUser($userid, 0, $password);
                if ($res) {
                    echo json_encode(array("code" => 0, "msg" => "success"));
                } else {
                    echo json_encode(array("code" => 400, "msg" => "update pwd error"));
                }
            }
        } else {
            if (!isset($_REQUEST['password']) || empty($_REQUEST['password'])) {
                $role = $_REQUEST['role'];
                $community = $_REQUEST['community'];
                $res = $usercom->updateUser($userid, 1, 0, $community, $role);
                if($res){
                    echo json_encode(array("code" => 0, "msg" => "success"));
                }else{
                    echo json_encode(array("code" => 400, "msg" => "update workeruser fail no pwd"));
                }
            } else {
                $password = $_REQUEST['password'];
                $password = md5($password);
                $res = $usercom->updateUser($userid, 1, $password, $community, $role);
                if($res){
                    echo json_encode(array("code" => 0, "msg" => "success"));
                }else{
                    echo json_encode(array("code" => 400, "msg" => "update workeruser fail have pwd"));
                }
            }
        }
        break;
    case 4://查
        $userid = $_REQUEST['userid'];
        $user = $usercom->getUserById($userid);
        if (!$user) {
            echo json_encode(array("code" => 400, "msg" => "search user failed"));
            break;
        }
        $result = array("code" => 0, "data" => array());
        $result["data"]['account'] = $user['account'];
        $result["data"]["community"] = $user['community_id'];
        $result["data"]["role"] = $user['role'];
        echo json_encode($result);
        break;
    default :
        echo json_encode(array("code" => 500, "msg" => "undefined error"));
        break;
}
exit();

